<template>
  <div class="content-container">
    <h3>发表评价</h3>
    <textarea placeholder="请输入您要BB的内容(最多120字)" maxlength="120" v-model="postMsg"></textarea>
    <mt-button type="primary" size="large" @click="postComment">发表评价</mt-button>
    <div class="content-box" v-for="(item,i) in contentList" :key="i">
      <p
        class="content-title"
      >第 {{i+1}} 楼&nbsp;&nbsp;用户:{{item.user_name}}}&nbsp;&nbsp;发表时间：{{item.add_time | dateFormat}}</p>
      <p class="content-body">{{item.content=='undefined'?"此用户很懒，什么也没留下":item.content}}</p>
    </div>
    <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      contentList: [], //评价列表
      pageIndex: 1, //默认页码为1
      postMsg: "" //发布评价的内容
    };
  },
  created() {
    this.getContent();
  },
  methods: {
    getContent() {
      this.$http
        .get("api/getcomments/" + this.id + "?pageindex=" + this.pageIndex)
        .then(result => {
          if (result.body.status == 0) {
            //concat是合并数据，防止点击更多是新数据覆盖旧的数据
            this.contentList = this.contentList.concat(result.body.message);
          }
        });
    },
    getMore() {
      this.pageIndex++;
      this.getContent();
    },
    postComment() {
      if (this.postMsg.trim().length == 0) {
          return Toast("评价内容不可以为空！")
      }
      //上传评价到服务器
      this.$http.post("api/postcomment/"+this.$route.params.id,{content:this.postMsg.trim()})
      .then(result =>{
          if(result.body.status==0){
              var cmt={
                  user_name:"匿名用户",
                  add_time:Date.now(),
                  content:this.postMsg
              }
              this.contentList.unshift(cmt);
              this.postMsg='';
          }
      })
    }
  },
  //调用记得传id
  props: ["id"]
};
</script>
<style lang="scss" scoped>
.content-container {
  h3 {
    font-size: 18px;
  }
  textarea {
    font-size: 13px;
  }
  .content-box {
    line-height: 30px;
    .content-title {
      background-color: #cccccc;
      margin: 10px 0;
    }
    .content-body {
      //段落缩进
      text-indent: 2em;
    }
  }
}
</style>
